var tcity = require("../../utils/citys");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //控制模态框
    hidden:false, 
    request_count:0,
    address_id: null, // 用户当前地址id
    money: 0, // 当前商品总价
    shopping_cars_ids: [], // 结算商品列表
    cars_ids: [], // 结算商品id列表
    address_List: [], // 用户地址列表
    address_current: {} // 用户当前地址
  },
  // 返回前一页
  go_back() {
    wx.navigateBack({
      delta: 1,
    })
  },
 //控制模态框显示
  control_hidden(){
    this.setData({
     request_count:this.data.request_count + 1,
    })
    console.log(this.data.request_count);
    if(this.data.request_count ==1){
      this.setData({
        hidden:true
      })
    }
  },
  // 去往地址管理页
  go_addressManage() {
    wx.navigateTo({
      url: '../userSet/addressManage/addressManage',
    })
  },
  // 生成订单，前往支付
  toPay() {
    console.log('生成订单');
    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/generateOrder',
      method: 'post',
      data: {
        "token": wx.getStorageSync('token'),
        "address_id": this.data.address_id,
        "money": this.data.money,
        "shopping_car_ids": this.data.cars_ids
      },
      success: (res) => {
        this.control_hidden()

        console.log(res);
        let timeStamp_tem = res.data.data.timeStamp;
        let nonceStr_tem = res.data.data.nonce_str;
        let package_tem = 'prepay_id=' + res.data.data.prepay_id;
        let paySign_tem = res.data.data.paySign;
        wx.requestPayment({
          nonceStr: nonceStr_tem,
          package: package_tem,
          paySign: paySign_tem,
          timeStamp: timeStamp_tem,
          signType: 'MD5',
          success: (res) => {
          this.control_hidden();
            console.log(res);
            wx.switchTab({
              url: '../gouwuche/gouwuche',
            });
          },
          fail: (err) => {
            console.log(err);
          }
        })
      },
      fail: (err) => {
        console.log(err);
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.address_id != undefined) {
      this.setData({
        address_id: options.address_id
      })
      wx.request({
        url: 'http://api_devs.wanxikeji.cn/api/userAddressList',
        data: {
          "token": wx.getStorageSync('token')
        },
        success: (res) => {
          console.log(res.data.data);
          // 整理返回的数据
          tcity.init(this);
          var cityData = this.data.cityData;
          var tempList=[];
          for(let i = 0; i < res.data.data.length; i++){
            let obj={};
            let temp1="";
            let temp2="";
            let temp3="";
            let temp4="";

            //筛选省
            temp1=cityData.filter((value)=>{
              if(value.code==res.data.data[i].procince) 
              return value;
            })[0];
            //筛选市
            temp2=(temp1.sub).filter((value)=>{
              if(value.code==res.data.data[i].city) 
              return value;
            })[0];
            //筛选区
            temp3=(temp2.sub).filter((value)=>{
              if(value.code==res.data.data[i].area) 
              return value;
            })[0];
          
            temp4=res.data.data[i].detailed;

            obj["address_id"]=res.data.data[i].address_id;
            obj["area"]=temp3.name;
            obj["city"]=temp2.name;
            obj["procince"]=temp1.name;
            obj["area_number"]=res.data.data[i].area;
            obj["city_number"]=res.data.data[i].city;
            obj["procince_number"]=res.data.data[i].procince;
            obj['created_time']=res.data.data[i].created_time;
            obj["default"]=res.data.data[i].default;
            obj["detailed"]=res.data.data[i].detailed;
            obj["name"]=res.data.data[i].name;
            obj["phone"]=res.data.data[i].phone;
            obj["status"]=res.data.data[i].status;
            obj["address"]=temp1.name+'-'+temp2.name+'-'+temp3.name+'-'+temp4;
            if (res.data.data[i].address_id == this.data.address_id) {
              obj['isChecked']=true;
            } else {
              obj['isChecked']=false
            }
            tempList.push(obj);
          }
          // 整理完毕，赋值到变量中
          this.setData({
            address_List: tempList
          })
          for (let i = 0; i < this.data.address_List.length; i++) {
            if (this.data.address_List[i].isChecked) {
              this.setData({
                address_current: JSON.parse(JSON.stringify(this.data.address_List[i]))
              })
              break;
            }
          }
          console.log(this.data.address_List,this.data.address_current);
          this.control_hidden()
        },
        fail: (err) => {
          console.log(err);
        }
      })
    }
    if (options.money != undefined) {
      this.setData({
        money: options.money
      })
    }
    console.log("options.shopping_cars_ids");
    console.log(options);
    console.log(options.shopping_cars_ids);
    if (options.shopping_cars_ids != undefined) {
      console.log("options.shopping_cars_ids");
      console.log(options.shopping_cars_ids);
      this.setData({
        shopping_cars_ids: JSON.parse(options.shopping_cars_ids)
      })
      let cards_ids_tem = []
      for (let i = 0; i < this.data.shopping_cars_ids.length; i++) {
        cards_ids_tem.push(this.data.shopping_cars_ids[i].shopping_car_id);
      }
      this.setData({
        cars_ids: cards_ids_tem
      })
    }
    console.log(this.data.address_id,this.data.money,this.data.shopping_cars_ids,this.data.cars_ids);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})